package com.efast.cafe.eureka.listener;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaRegistryAvailableEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaServerStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.netflix.appinfo.InstanceInfo;
@ConditionalOnProperty(
        name="event.enable",
        havingValue="true"
)
@Component
public class EurekaStateChangeListener {
	
	private final Logger logger = LoggerFactory.getLogger(EurekaStateChangeListener.class);

	
	 @EventListener
	 public void listen(EurekaInstanceCanceledEvent eurekaInstanceCanceledEvent) {
	 //服务下线事件
	 String appName = eurekaInstanceCanceledEvent.getAppName();
	 String serverId = eurekaInstanceCanceledEvent.getServerId();
		 logger.error("服务下线告警：appName:"+appName+",serverId:"+serverId);
	 }
	 
	 @EventListener
	 public void listen(EurekaInstanceRegisteredEvent event) {
	 //服务注册事件
	 InstanceInfo instanceInfo = event.getInstanceInfo();
	 System.out.println(instanceInfo);
	 logger.info("EurekaInstanceRegisteredEvent,appName:"+instanceInfo.getAppName());
	 }
	 
	 @EventListener
	 public void listen(EurekaInstanceRenewedEvent event) {
	 //服务续约事件
	 event.getAppName();
	 event.getServerId();
	 //System.out.println("EurekaInstanceRenewedEvent");
	 }
	 
	 @EventListener
	 public void listen(EurekaRegistryAvailableEvent event) {
	 //System.out.println("EurekaRegistryAvailableEvent");
	 }
	 
	 @EventListener
	 public void listen(EurekaServerStartedEvent event) {
	 //System.out.println("EurekaServerStartedEvent");
	 //Server启动
	 }
}
